package com.dao;
import com.util.*;
import java.util.List;

import com.model.Product;
import com.util.JdbcUtil;
public class ProductDao {
	JdbcUtil jdbc = new JdbcUtil();

    public List<Product> selectproduct(){
    String sql = "select * from product";
	    List<Product> products = jdbc.queryPreparedStatement(sql, Product.class);
	    return products;
	    
    }
	//出售商品查询（用户）,条件从传递对象中取出
	public List<Product> query_user(Product product,int cp,int pagesize){
		List<Product> products = null;
		//最内层代码
		StringBuffer sql = new StringBuffer("select * from product  where 1=1 ");
		//添加查询的条件1
		if(product.getKid()!=null){
			sql.append(" and kid="+product.getKid());
		}
		//添加查询的条件2
		if(product.getPname()!=null){
			sql.append(" and pname like '%"+product.getPname()+"%'");
		}
		sql.append(" order by pid ");
		//外层分页语句
		StringBuffer sb = new StringBuffer();
		sb.append(sql);
		sb.append(" limit "+(cp-1)*pagesize+","+(cp)*pagesize);
		//System.out.println(sb.toString());
		products = jdbc.queryPreparedStatement(sb.toString(), Product.class);
		jdbc.close();
		return products;
	}
	
	public List<Product> query_user2(Product product){
		List<Product> products = null;
		//最内层代码
		StringBuffer sql = new StringBuffer("select * from product  where 1=1 ");
		//添加查询的条件
		if(product.getKid()!=null){
			sql.append(" and kid="+product.getKid());
		}
		if(product.getPname()!=null&& !product.getPname().equals("")){
			sql.append(" and pname like '%"+product.getPname()+"%'");
		}
		products = jdbc.queryPreparedStatement(sql.toString(), Product.class);
		jdbc.close();
		return products;
	}
	//查询总条数 根据需求变化,与query_user搭配使用
	public int count_user(Product product){
		return query_user2(product).size();
	}
    
   
}
